package com.xlabs.client.richtext;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.Widget;

/**
 * An Image button that changes its state when clicked.
 * @author ali.akhavan
 */
public class PersistentImageButton extends ImageButton
{
	/**
	 * Whether the button is in pressed mode.
	 */
	private boolean pressed;
	
	public PersistentImageButton(String url, String tooltip)
	{
		super(url, tooltip);
		addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				PersistentImageButton.this.pressed = !pressed;
			}
		});
	}

	/**
	 * Setter for {@link #pressed}
	 * @param pressed Whether the button is pressed.
	 */
	public void setPressed(boolean pressed)
	{
		this.pressed = pressed;
	}

	/**
	 * Getter for {@link #pressed}
	 * @return Whether the button is pressed.
	 */
	public boolean isPressed()
	{
		return pressed;
	}
	
	public void onMouseOver(MouseOverEvent event)
	{
		if (!pressed)
		{
			setStyleName("ImageButton-over");
		}
	}

	public void onMouseMove(MouseMoveEvent event)
	{
		if (!pressed)
		{
			setStyleName("ImageButton-over");
		}
	}

	public void onMouseOut(MouseOutEvent event)
	{
		if (!pressed)
		{
			setStyleName("ImageButton");
		}
	}

	public void onMouseDown(MouseDownEvent event)
	{
		if (!pressed)
		{
			setStyleName("ImageButton-pressed");
		}
		else
		{
			setStyleName("ImageButton");
		}
	}

	public void onMouseUp(MouseUpEvent event)
	{
		if (pressed)
		{
			setStyleName("ImageButton-over");
		}
		else
		{
			setStyleName("ImageButton-pressed");
		}
	}

}
